*************************************************************************
*																		*
*				Discrete Survival Analysis 								*
*																		*
*************************************************************************

* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger 
* Date: June, 2017

set more off


/* Because built in options of coefplot do not allow desired
graph we manually store predictive results of multilpe margins-commands
in matrices, manually compute 95%-confience intervalls and plot results 
with coefplot from these matrices */
				

*------------------------------------------------------
* Figure 12.4
*--------------------------------------------------------

clear matrix

foreach y in a b { 											//loop over years
quietly logit `y'_decided  ///
	i.fd_`y'_panelcongtalk i.fd_`y'_panelcongcontact ///
	i.fd_`y'_panelcongtvduell ///
	i.fd_`y'_panelcongard i.fd_`y'_panelcongzdf ///
	fd_`y'_talk fd_`y'_countcontact fd_`y'_tvduell ///
	fd_`y'_paper ///
	fd_`y'_oeff ibn.wavefd, noconstant

margins i.fd_`y'_panelcongtalk i.fd_`y'_panelcongcontact ///
	i.fd_`y'_panelcongard i.fd_`y'_panelcongzdf, post
	
mat `y'b = e(b)'
mat `y'V = r(V)
mat `y'se = (sqrt(`y'V[1,1]+`y'V[2,2]-2*`y'V[2,1]) \ sqrt(`y'V[3,3]+`y'V[4,4]-2*`y'V[4,3]) \ ///
	sqrt(`y'V[5,5]+`y'V[6,6]-2*`y'V[6,5]) \ sqrt(`y'V[7,7]+`y'V[8,8]-2*`y'V[8,7]))
}


* TV debate in separate margin command because variation only in one wave
quietly logit a_decided  ///
	i.fd_a_panelcongtalk i.fd_a_panelcongcontact ///
	i.fd_a_panelcongtvduell ///
	i.fd_a_panelcongard i.fd_a_panelcongzdf ///
	fd_a_talk fd_a_countcontact fd_a_tvduell ///
	fd_a_paper ///
	fd_a_oeff ibn.wavefd, noconstant
margins fd_a_panelcongtvduell, at(wave=(6)) post  //2009: W6

mat abdebate = e(b)'		
mat adebateV = r(V)
mat adebatese = (sqrt(adebateV[1,1]+adebateV[2,2]-2*adebateV[2,1]))

quietly logit b_decided  ///
	i.fd_b_panelcongtalk i.fd_b_panelcongcontact ///
	i.fd_b_panelcongtvduell ///
	i.fd_b_panelcongard i.fd_b_panelcongzdf ///
	fd_b_talk fd_b_countcontact fd_b_tvduell ///
	fd_b_paper ///
	fd_b_oeff ibn.wavefd, noconstant
margins fd_b_panelcongtvduell, at(wave=(5)) post  //2013: W5


mat bbdebate = e(b)'		
mat bdebateV = r(V)
mat bdebatese = (sqrt(bdebateV[1,1]+bdebateV[2,2]-2*bdebateV[2,1]))



* put together target matrix from which plot is produced:
* 1st column: probability difference
* 2st column: lower bound 95% probability difference
* 3st column: upper bound 95% probability difference
* rows: talk, party contact, debate, ard, zdf

foreach y in a b {
mat diff`y' = J(5,3,.) 

* prob diff
mat diff`y'[1,1] = `y'b[2,1]-`y'b[1,1]   
mat diff`y'[2,1] = `y'b[4,1]-`y'b[3,1] 
mat diff`y'[3,1] = `y'bdebate[2,1]-`y'bdebate[1,1] 
mat diff`y'[4,1] = `y'b[6,1]-`y'b[5,1]   
mat diff`y'[5,1] = `y'b[8,1]-`y'b[7,1]   

* lower bound (95%) of prob diff
mat diff`y'[1,2] = (`y'b[2,1]-`y'b[1,1]) - 1.96*`y'se[1,1] 
mat diff`y'[2,2] = (`y'b[4,1]-`y'b[3,1]) - 1.96*`y'se[2,1]
mat diff`y'[3,2] = (`y'bdebate[2,1]-`y'bdebate[1,1]) - 1.96*`y'debatese 
mat diff`y'[4,2] = (`y'b[6,1]-`y'b[5,1])  - 1.96*`y'se[3,1] 
mat diff`y'[5,2] = (`y'b[8,1]-`y'b[7,1]) - 1.96*`y'se[4,1]

* upper bound (95%) of prob diff
mat diff`y'[1,3] = (`y'b[2,1]-`y'b[1,1]) + 1.96*`y'se[1,1] 
mat diff`y'[2,3] = (`y'b[4,1]-`y'b[3,1]) + 1.96*`y'se[2,1]
mat diff`y'[3,3] = (`y'bdebate[2,1]-`y'bdebate[1,1]) + 1.96*`y'debatese 
mat diff`y'[4,3] = (`y'b[6,1]-`y'b[5,1]) + 1.96*`y'se[3,1] 
mat diff`y'[5,3] = (`y'b[8,1]-`y'b[7,1]) + 1.96*`y'se[4,1]
}



* Graph
graph set window fontface "Arial"
coefplot	(matrix(diffa[,1]), ci((diffa[,2] diffa[,3])) msymbol(diamond))  ///
			(matrix(diffb[,1]), ci((diffb[,2] diffb[,3])) msymbol(O)), ///
			coeflabel(r1="Talk" r2="Party contact" r3="Debate" r4="ARD" r5="ZDF") ///
			xtitle("Discrete change in average predicted probability", margin(medium)) ///
			legend(label(2 "2009") label(4 "2013")) scheme(s1mono) xline(0) ///
			xlabel(-0.1(0.05)0.3)
graph export fig12_4_20170619.png, replace width(2000) height(1000)



*-----------------------------------------------------
* Figure 12.5
*-------------------------------------------------------


clear matrix

foreach var of varlist diff_panelcongtalk diff_panelcongcontact ///
	 diff_panelcongard diff_panelcongzdf {
quietly mlogit help_decided diff_panelcongtalk ///
	diff_panelcongcontact diff_panelcongtvduell ///
	diff_panelcongard diff_panelcongzdf ///
	diff_paneltalk diff_panelcountcontact ///
	diff_panelpaper diff_tvduell ///
	diff_paneloeff ibn.wavem, noconstant
margins, at(`var'=(0 1)) ///
	predict(outcome(2)) post  //outcome 2 = only 2013 decided	
mat b`var' = e(b)
mat V`var' = r(V)
mat se`var' = (sqrt(V`var'[1,1]+V`var'[2,2]-2*V`var'[2,1])) 	
}


quietly mlogit help_decided diff_panelcongtalk ///
	diff_panelcongcontact diff_panelcongtvduell ///
	diff_panelcongard diff_panelcongzdf ///
	diff_paneltalk diff_panelcountcontact ///
	diff_panelpaper diff_tvduell ///
	diff_paneloeff ibn.wavem, noconstant
margins, at(diff_panelcongtvduell=(0 1) wave=5) ///
	predict(outcome(2)) post 
mat bdiff_panelcongtvduell = e(b)
mat Vdiff_panelcongtvduell = r(V)
mat sediff_panelcongtvduell = (sqrt(Vdiff_panelcongtvduell[1,1]+ ///
	Vdiff_panelcongtvduell[2,2]-2*Vdiff_panelcongtvduell[2,1])) 	

	
* target matrix from which plot is produced:
* 1st column: probability difference
* 2st column: lower bound 95% probability difference
* 3st column: upper bound 95% probability difference
* rows: talk, party contact, debate, ard, zdf

mat target = J(5,3,.)

* prob diff
mat target[1,1] = bdiff_panelcongtalk[1,2]-bdiff_panelcongtalk[1,1]
mat target[2,1] = bdiff_panelcongcontact[1,2]-bdiff_panelcongcontact[1,1]
mat target[3,1] = bdiff_panelcongtvduell[1,2]-bdiff_panelcongtvduell[1,1]
mat target[4,1] = bdiff_panelcongard[1,2]-bdiff_panelcongard[1,1]
mat target[5,1] = bdiff_panelcongzdf[1,2]-bdiff_panelcongzdf[1,1]

* lower bound 95% of prob diff
mat target[1,2] = (bdiff_panelcongtalk[1,2]-bdiff_panelcongtalk[1,1])-1.96*sediff_panelcongtalk
mat target[2,2] = (bdiff_panelcongcontact[1,2]-bdiff_panelcongcontact[1,1])-1.96*sediff_panelcongcontact
mat target[3,2] = (bdiff_panelcongtvduell[1,2]-bdiff_panelcongtvduell[1,1])-1.96*sediff_panelcongtvduell
mat target[4,2] = (bdiff_panelcongard[1,2]-bdiff_panelcongard[1,1])-1.96*sediff_panelcongard
mat target[5,2] = (bdiff_panelcongzdf[1,2]-bdiff_panelcongzdf[1,1])-1.96*sediff_panelcongzdf

* upper bound 95% of probdiff
mat target[1,3] = (bdiff_panelcongtalk[1,2]-bdiff_panelcongtalk[1,1])+1.96*sediff_panelcongtalk
mat target[2,3] = (bdiff_panelcongcontact[1,2]-bdiff_panelcongcontact[1,1])+1.96*sediff_panelcongcontact
mat target[3,3] = (bdiff_panelcongtvduell[1,2]-bdiff_panelcongtvduell[1,1])+1.96*sediff_panelcongtvduell
mat target[4,3] = (bdiff_panelcongard[1,2]-bdiff_panelcongard[1,1])+1.96*sediff_panelcongard
mat target[5,3] = (bdiff_panelcongzdf[1,2]-bdiff_panelcongzdf[1,1])+1.96*sediff_panelcongzdf


* Graph
coefplot (matrix(target[,1]), ci((target[,2] target[,3]))), ///
		coeflabel(r1="Talk" r2="Party contact" r3="Debate" r4="ARD" r5="ZDF") ///
		xtitle("Discrete change in average predicted probability", margin(medium)) scheme(s1mono) ///
		xlabel(-.1(0.05)0.25) xline(0)

graph export "fig12_5_20170619.png", replace width(2000) height(1000)

